package com.extension.company.admin.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.extension.company.admin.R;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;
import com.umeng.socialize.shareboard.ShareBoardConfig;
import com.umeng.socialize.shareboard.SnsPlatform;
import com.umeng.socialize.utils.ShareBoardlistener;

import java.lang.ref.WeakReference;

/**
 * zwk
 * 功能：一鍵分享功能
 */
public class GeneralizeShareActivity extends AppCompatActivity implements View.OnClickListener {

	private ShareAction mGeneralizeShareAction;
	private UMShareListener mShareListener;

	/**
	 * 复制转发
	 */
	private SHARE_MEDIA[] copyTransmitShare = {
			SHARE_MEDIA.SINA,
			SHARE_MEDIA.QQ,
			SHARE_MEDIA.WEIXIN,
			SHARE_MEDIA.QZONE,
			SHARE_MEDIA.WEIXIN_CIRCLE
	};

	/**
	 * 一键转发
	 */
	private SHARE_MEDIA[] oneClickShare = {
			SHARE_MEDIA.WEIXIN_CIRCLE,
			SHARE_MEDIA.QZONE
	};


	public static void Internet(Activity activity) {
		Intent intent = new Intent(activity, GeneralizeShareActivity.class);
		activity.startActivity(intent);


	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_share);

		findViewById(R.id.copy_share).setOnClickListener(this);
		findViewById(R.id.share_btn).setOnClickListener(this);

		mShareListener = new CustomShareListener(this);


	}


	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.copy_share:
				mGeneralizeShareAction = new ShareAction(GeneralizeShareActivity.this)
						.setDisplayList(copyTransmitShare)                 // 设置分享面板的内容
						.setShareboardclickCallback(new ShareBoardlistener() {  // 分享面板设置点击监听
							@Override
							public void onclick(SnsPlatform snsPlatform, SHARE_MEDIA share_media) {

								UMWeb web = new UMWeb("http://mobile.umeng.com/social");
								web.setTitle("来自分享面板标题");
								web.setDescription("来自分享面板内容");
								web.setThumb(new UMImage(GeneralizeShareActivity.this, R.drawable.xiaomi));
								new ShareAction(GeneralizeShareActivity.this).withMedia(web)
										.setPlatform(share_media)
										.setCallback(mShareListener)
										.share();
							}
						});
				mGeneralizeShareAction.open();
				break;

			case R.id.share_btn:
				ShareBoardConfig config = new ShareBoardConfig();
				config.setShareboardPostion(ShareBoardConfig.SHAREBOARD_POSITION_CENTER); // 居中显示分享面板

				mGeneralizeShareAction = new ShareAction(GeneralizeShareActivity.this)
						.setDisplayList(oneClickShare)                 // 设置分享面板的内容
						.setShareboardclickCallback(new ShareBoardlistener() {  // 分享面板设置点击监听
							@Override
							public void onclick(SnsPlatform snsPlatform, SHARE_MEDIA share_media) {

								UMImage imageurl = new UMImage(GeneralizeShareActivity.this, "http://mobile.umeng.com/social");
								imageurl.setThumb(new UMImage(GeneralizeShareActivity.this, R.drawable.xiaomi));
								new ShareAction(GeneralizeShareActivity.this).withMedia(imageurl)
										.setPlatform(share_media)
										.setCallback(mShareListener).share();

							}
						});
				mGeneralizeShareAction.open();


				break;
			default:
				break;

		}

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		/** attention to this below ,must add this**/
		UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
	}

	/**
	 * UM 分享监听的方法
	 */
	private static class CustomShareListener implements UMShareListener {

		private WeakReference<GeneralizeShareActivity> mActivity;

		private CustomShareListener(GeneralizeShareActivity activity) {
			mActivity = new WeakReference(activity);
		}

		@Override
		public void onStart(SHARE_MEDIA share_media) {

		}

		@Override
		public void onResult(SHARE_MEDIA share_media) {

		}

		@Override
		public void onError(SHARE_MEDIA share_media, Throwable throwable) {

		}

		@Override
		public void onCancel(SHARE_MEDIA share_media) {

		}
	}

	/**
	 * 屏幕横竖屏切换时避免出现window leak的问题
	 */
	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		mGeneralizeShareAction.close();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		UMShareAPI.get(this).release();
	}
}
